package com.dgut.demo.service.impl;

import com.dgut.demo.bean.GoodsMessage;
import com.dgut.demo.bean.User;
import com.dgut.demo.bean.VipMessage;
import com.dgut.demo.controller.LogsController;
import com.dgut.demo.mapper.GoodsMapper;
import com.dgut.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther 虎
 * @Data 2021/2/4 23:27
 * @Version 1.0
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private User user;
    @Autowired
    private LogsController logsController;

    @Override
    public List<GoodsMessage> getAllGoodsMessage() {
        return goodsMapper.getAllGoodsMessage();
    }

    @Override
    public int addOneGoodsMessage(GoodsMessage goodsMessage) {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        int random = (int)(Math.random()*10);
        String gid='3'+ dateFormat.format(date) + random;
        goodsMessage.setGid(gid);
        int flag;
        flag = goodsMapper.addOneGoodsMessage(goodsMessage);
        if(flag == 1){
            //log
            String actions = "新增商品【"+goodsMapper.getGoodsMessageById(gid).toString()+"】";
            String userId = user.getUserId();
            logsController.addOneLogsMessage(actions,userId);
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteOneGoodsMessage(String gid) {
        GoodsMessage goodsMessage = goodsMapper.getGoodsMessageById(gid).get(0);
        //log
        String actions = "删除商品【"+goodsMessage.toString()+"】";
        String userId = user.getUserId();
        logsController.addOneLogsMessage(actions,userId);

        return goodsMapper.deleteOneGoodsMessage(gid);
    }

    @Override
    public int updateOneGoodsMessage(GoodsMessage goodsMessage) {
        GoodsMessage goodsMessage1 = goodsMapper.getGoodsMessageById(goodsMessage.getGid()).get(0);
        int flag = goodsMapper.updateOneGoodsMessage(goodsMessage);
        if(flag == 1){
            GoodsMessage goodsMessage2 = goodsMapper.getGoodsMessageById(goodsMessage.getGid()).get(0);
            //log
            String actions = "更新商品信息【"+goodsMessage1.toString()+"】--》【"+goodsMessage2.toString()+"】";
            String userId = user.getUserId();
            logsController.addOneLogsMessage(actions,userId);
        }
        return flag;
    }

    @Override
    public List<GoodsMessage> getGoodsMessageById(String gid) {
        return goodsMapper.getGoodsMessageById(gid);
    }

    @Override
    public List<GoodsMessage> getGoodsMessageByName(String gname) {
        return goodsMapper.getGoodsMessageByName(gname);
    }

    @Override
    public List<GoodsMessage> getGoodsMessageByPrice(String gprice) {
        return goodsMapper.getGoodsMessageByPrice(gprice);
    }

    @Override
    public List<GoodsMessage> getGoodsMessageByType(String gtype) {
        return goodsMapper.getGoodsMessageByType(gtype);
    }
}
